
#import "GamesToPlayTableViewController.h"
#import "GamesToPlayTableViewCell.h"
#import "Reactor.h"

@interface GamesToPlayTableViewController ()

@property NSMutableArray *games;
@property Settings *settings;

@end

@implementation GamesToPlayTableViewController

- (void)viewDidLoad {
    
    self.settings = [Settings sharedSettings];
    self.games = [[self.settings allGames] mutableCopy];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"GamesToPlayTableViewCell" bundle:nil] forCellReuseIdentifier:@"game"];
    
    //Set Select/Deselect button
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 40, 40)];
    [backButton setContentMode:UIViewContentModeScaleAspectFit];
    [backButton setBackgroundImage:[UIImage imageNamed:@"backArrow.png"] forState:UIControlStateNormal];
    UIBarButtonItem *navBackButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = navBackButton;
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem *deselectAll = [[UIBarButtonItem alloc]
                                    initWithTitle:@"Deselect All"
                                    style:UIBarButtonItemStylePlain
                                    target:self
                                    action:@selector(deselectAll)];
    
    [deselectAll setTitleTextAttributes:@{
                                          NSFontAttributeName: [UIFont fontWithName:@"GillSans-Light" size:26.0],
                                          NSForegroundColorAttributeName: [UIColor redColor]
                                          } forState:UIControlStateNormal];
    
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.75];
    self.navigationItem.rightBarButtonItem = deselectAll;
    
}

//Select All Games
-(void)selectAll {
    
    for (int i = 0; i < [self.games count]; i++) {
        [self.settings addGame:[self.games objectAtIndex:i]];
    }
    [self.tableView reloadData];
    [self.navigationItem.rightBarButtonItem setTitle:@"Deselect All"];
    [self.navigationItem.rightBarButtonItem setAction:@selector(deselectAll)];
}

//Deselect All games
-(void)deselectAll {
    
    for (int i = [self.settings.gamesToPlay count] -1; i >= 0; i--) {
        [self.settings removeGame:[self.settings.gamesToPlay objectAtIndex:0]];
    }
    [self.tableView reloadData];
    [self.navigationItem.rightBarButtonItem setTitle:@"Select All"];
    [self.navigationItem.rightBarButtonItem setAction:@selector(selectAll)];
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([self.settings.gamesToPlay count] == 0) {
        [self.navigationItem.rightBarButtonItem setTitle:@"Select All"];
        [self.navigationItem.rightBarButtonItem setAction:@selector(selectAll)];
    }
    
    //set nav bar
    [self.navigationController setNavigationBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.navigationController.navigationBar.bounds = CGRectMake(0,0,768,100);
    
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.translucent = NO;
    
    //decide to select or deselect (button title)
    int uncheckedCount = 1;
    GamesToPlayTableViewCell *cell = (GamesToPlayTableViewCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    for (int i ; i < [self.games count]; i++) {
        cell = (GamesToPlayTableViewCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if (!cell.checked) {
            uncheckedCount++;
        }
    }
    if (uncheckedCount == [self.games count]) {
        [self.navigationItem.rightBarButtonItem setTitle:@"SelectAll"];
        [self.navigationItem.rightBarButtonItem setAction:@selector(selectAll)];
        
    }
}

-(void)backButtonPressed:(BOOL)pressed {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //save to settings
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.settings.gamesToPlay forKey:@"gamesToPlay"];
}
-(BOOL)prefersStatusBarHidden { return YES; }

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.games count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GamesToPlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"game" forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[GamesToPlayTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"game"];
    }
    cell.title.text = [self.games objectAtIndex:indexPath.row];
    
    //set cell checkboxes
    if ([self.settings.gamesToPlay containsObject:[self.games objectAtIndex:indexPath.row]]) {
        cell.checkBox.image = [UIImage imageNamed:@"check.png"];
        cell.checked = YES;
    }
    else {
        cell.checkBox.image = nil;
        cell.checked = NO;
    }
    
    cell.title.text = [self.games objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //check/uncheck cell
    GamesToPlayTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.checked) {
        cell.checked = NO;
        [self.settings removeGame:[self.games objectAtIndex:indexPath.row]];
        [tableView reloadData];
    }
    else {
        cell.checked = YES;
        [self.settings addGame:[self.games objectAtIndex:indexPath.row]];
        [tableView reloadData];
    }
}
@end
